package rabbitmq.workqueue;

import com.rabbitmq.client.*;
import org.pb.basic.util.ConnectionUtils;

import java.io.IOException;

/**
 * 工作模式的消费者1
 * @author pb
 */
public class Comsumer1 {

    /**
     * 声明队列名
     */
    private static final String QUEUE = "work_queue";

    public static void main(String[] args) throws Exception {
        // 获取连接
        Connection connection = ConnectionUtils.getConnection();
        // 创建连接通道
        Channel channel = connection.createChannel();
        // 创建通道的时候，声明通道的消息获取方式为完成了消息处理后，才重新获取若干条消息
        channel.basicQos(1);
        // 创建消费者的处理逻辑
        DefaultConsumer comsumer = new DefaultConsumer(channel) {
            /**
             * 消费者的消息处理逻辑
             * @param consumerTag
             * @param envelope
             * @param properties
             * @param body
             * @throws IOException
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String exchange = envelope.getExchange();
                String routingKey = envelope.getRoutingKey();
                long id = envelope.getDeliveryTag();
                String msg = new String(body, "utf8");
                System.out.println(msg);
            }
        };

        // 消费者监听通道
        channel.basicConsume(QUEUE, true, comsumer);
    }

}
